package cate.game.pvp.gradepvp;

import cate.common.table.d.GDGradePvp;
import cate.game.GameBody;
import cate.game.db.mongo.kv.KvMongo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.annotation.Transient;


@Slf4j
public class GradePvpStatus extends KvMongo {
    @Transient
    @JsonIgnore
    private GameBody game;
    @NoteField(value = "当前赛季")
    public int season;

    @NoteField(value = "上半是否发过奖励")
    public boolean firstHalfSentReward;

    @NoteField(value = "下半赛季是否发过奖励")
    public boolean secondHalfSentReward;

    @NoteField(value = "赛季阶段")
    public int period;

    @NoteField(value = "功能开启时间")
    public long startTime;

    @Override
    public String getKeyPrefix() {
        return KvMongo.KEY_GRADE_PVP;
    }

    @Override
    public void initialize() {
        if (startTime == 0) {
            startTime = GDGradePvp.START_TIME;
        }
    }

    public void initialize(GameBody gameBody) {
        this.game = gameBody;
        this.initialize();
    }

    public long seasonStartTime() {
        return GradePvpHelper.calcCurSeasonStartTime(startTime);
    }

    public void tick() {
        if (startTime == 0) {
            return;
        }
        int currentSeason = GradePvpHelper.calcCurrentSeason(startTime);
        if (currentSeason == 0) {
            season = 0;
            firstHalfSentReward = false;
            secondHalfSentReward = false;
            period = 0;
            return;
        }
        // 赛季判断
        if (season != currentSeason) {
            season = currentSeason;
            log.info("进入新赛季：S{}", currentSeason);
            game.role.activeForOnline(role -> role.getRes().kingSacrifice.checkSeason(false));
        }
        // 允许宕机很久导致的状态多次变化
        int times = 0;
        GradePvpPeriod currentPeriod = GradePvpHelper.currentSeasonPeriod(startTime);
        if (period != currentPeriod.ordinal()) {
            log.info("马上进入赛季{}阶段", currentPeriod);
            GradePvpPeriod st = GradePvpPeriod.values()[period];
            while (currentPeriod.ordinal() != st.ordinal()) {
                log.info("当前为赛季{}阶段",st);
                st = GradePvpPeriod.getNextStatus(st);
                period = st.ordinal();
                executeStatus(st);
                times ++;
                //如果20次还没有结束循环，就放弃追，强制改成当前状态,防止死循环
                if(times > 10){
                    period = currentPeriod.ordinal();
                    break;
                }
            }
        }
    }

    private void executeStatus(GradePvpPeriod period) {
        if (period.equals(GradePvpPeriod.SECOND_HALF_SETTLE)) {
            this.secondHalfSentReward = true;
            game.role.activeForOnline(role -> role.getPvp().gradePvp.checkSeason());
        } else if (period.equals(GradePvpPeriod.FIRST_HALF_SETTLE)) {
            this.firstHalfSentReward = true;
            game.role.activeForOnline(role -> role.getPvp().gradePvp.checkSeason());
        } else if (period.equals(GradePvpPeriod.FIRST_HALF)) {
            this.firstHalfSentReward = false;
            this.secondHalfSentReward = false;
            game.role.activeForOnline(
                    role -> {
                        role.getPvp().gradePvp.checkSeason();
                    });
        }
    }
}
